package com.sq.universe.system.service.impl;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.sq.universe.base.entity.RespEntity;
import com.sq.universe.base.service.impl.BaseServiceImpl;
import com.sq.universe.base.utils.AuthUtils;
import com.sq.universe.system.dao.UserDao;
import com.sq.universe.system.dao.UserRoleDao;
import com.sq.universe.system.entity.UserEntity;
import com.sq.universe.system.entity.UserRoleEntity;
import com.sq.universe.system.service.SystemService;
import com.sq.universe.system.service.UserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author 孙乾 2020-07-08 23:36:45
*/
@Service
public class SystemServiceImpl extends BaseServiceImpl<UserEntity, UserDao> implements SystemService {

    @Autowired
    private UserDao userDao;


    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    private RestTemplate restTemplate;


    public List<Map> getServiceIps(String service)  {
        Map respEntity = (Map)restTemplate.getForObject("http://universe-gateway/gateWay/getGateWayConfig",Map.class);
        List<Map> serviceList = new ArrayList<>();
        List<ServiceInstance> allInstances = null;
        try {
            allInstances = discoveryClient.getInstances(service);
            allInstances.forEach(data->{
                Map row =  new HashMap();
                row.put("ip",data.getHost());
                row.put("port",data.getPort());
                row.put("path",respEntity.get(service));
                row.put("fullPath","http://" + data.getHost() + ":" + data.getPort() +  respEntity.get(service).toString());
                serviceList.add(row);
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
        return serviceList;
    }

    public List<Map> getServiceIps(String service, JSONObject params)  {
        Map respEntity = (Map)restTemplate.getForObject("http://universe-gateway/gateWay/getGateWayConfig",Map.class);
        List<Map> serviceList = new ArrayList<>();
        List<ServiceInstance> allInstances = null;
        try {
            allInstances = discoveryClient.getInstances(service);
            for (int i = 0; i < allInstances.size(); i++) {
                ServiceInstance data =  allInstances.get(i);
                Map row =  new HashMap();
                row.put("ip",data.getHost());
                row.put("port",data.getPort());
                row.put("key",data.getHost() + ":" + data.getPort());
                row.put("name",data.getHost() + ":" + data.getPort());
                row.put("path",respEntity.get(service));
                String url = "http://" + data.getHost() + ":" + data.getPort() +  respEntity.get(service).toString() + "//base/log/getLogs?X-Token=" + AuthUtils.getToken();
                String body = HttpUtil.createPost(url).body(JSONUtil.toJsonStr(params)).execute().body();
                RespEntity repFiles = JSONUtil.toBean(body, RespEntity.class);
                JSONArray fileArr = JSONUtil.parseArray(repFiles.getData());
                for (int j = 0; j < fileArr.size(); j++) {
                    fileArr.getJSONObject(j).set("fullPath","http://" + data.getHost() + ":" + data.getPort() +  respEntity.get(service).toString());
                }
                row.put("children",fileArr);
                serviceList.add(row);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return serviceList;
    }

    public List<String> getServices(){
        return discoveryClient.getServices();
    }

    @Override
    public List<Map<String,Object>> getLogs(JSONObject data) {
        List<String> serviceList = discoveryClient.getServices();
        List<Map<String,Object>> result  = new ArrayList<>();
        for (int i = 0; i < serviceList.size(); i++) {
            if(serviceList.get(i).contains("gateway")){
                continue;
            }
            Map<String,Object> row = new HashMap<>();
            row.put("key",serviceList.get(i));
            row.put("name",serviceList.get(i));
            row.put("children",this.getServiceIps(serviceList.get(i),data));
            result.add(row);
        }
        return result;
    }
}
